package com.ruoyi.common.utils;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.stream.Stream;

/**
 * @ClassName UpLoadUtils
 * @Description TODO 文件上传
 * @Author luguanqing
 * @Date 2021/4/7 8:53
 * @Version 1.0
 */
@Api(value="文件上传",description = "文件上传接口")
@RestController
@RequestMapping("/back/upload")
public class UpLoadUtils {

    /**
     * 文件上传
     */
    @ApiOperation(value="文件上传")
    @Log(title = "文件上传", businessType = BusinessType.INSERT)
    @PostMapping("/uploadFile")
    public AjaxResult upload(MultipartFile file) throws IOException
    {
        if (!file.isEmpty())
        {
            String fileName = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
            if(StringUtils.isNotEmpty(fileName)){
                AjaxResult ajax = AjaxResult.success();
                ajax.put("fileName", fileName);
                return ajax;
            }
        }
        return AjaxResult.error("上传文件异常，请联系管理员");
    }

    /**
     * 音视频上传
     */
    @ApiOperation(value="音视频上传")
    @Log(title = "音视频上传", businessType = BusinessType.INSERT)
    @PostMapping("/uploadVideo")
    public AjaxResult uploadVideo(MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            String fileName = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file, MimeTypeUtils.MEDIA_EXTENSION);
            if(StringUtils.isNotEmpty(fileName)){
                AjaxResult ajax = AjaxResult.success();
                ajax.put("fileName", fileName);
                return ajax;
            }
        }
        return AjaxResult.error("上传文件异常，请联系管理员");

    }

    public static void merge(String targetFile, String folder) {
        try {
            Files.createFile(Paths.get(targetFile));
            Files.list(Paths.get(folder))
                    .filter(path -> path.getFileName().toString().contains("-"))
                    .sorted((o1, o2) -> {
                        String p1 = o1.getFileName().toString();
                        String p2 = o2.getFileName().toString();
                        int i1 = p1.lastIndexOf("-");
                        int i2 = p2.lastIndexOf("-");
                        return Integer.valueOf(p2.substring(i2 + 1, i2 + 2)).compareTo(Integer.valueOf(p1.substring(i1+ 1, i2 + 2)));
                    })
                    .forEach(path -> {
                        try {
                            //以追加的形式写入文件
                            Files.write(Paths.get(targetFile), Files.readAllBytes(path), StandardOpenOption.APPEND);
                            //合并后删除该块
                            Files.delete(path);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
